import { changeColor, createDataMap } from "./ts/utils"
import fs from 'fs'
import path from "path"

function createCustom() {
    let keys = createDataMap().map(key => {
        return createKey(key)
    })
    let keysContent = ''
    keys.forEach(content => {
        keysContent += content
    })
    let custom = `
import { line } from "../../interface/excel"
import { dataMap } from "../ts/utils"
    
/**
 * @desc 用户自定义输出
 * @param sourceKey 数据源, 头行的key值
 * @param line 每行的数据
 * @PS 如果进行了自定义输出, 必须将值进行return
 * @returns line
 */
export function returnLine(
    sourceKey: line, 
    line: line
): line {
    return dataMap.map((key: string) => {
        // dataMap 为模板的头行
        // 数据的index列下标
        let index = sourceKey.findIndex((value: string) => value === key)
        let value = line[index]
        ${keysContent}
        return value
    })
}`
    return custom
}

function createKey(key: string): string {
    return `        
        if(key === '${key}'){
            /**
            * @key ${key}
            * @returns return 自定义内容
            **/     
            value = '自定义内容'
            return value
        }
    `
}

let content = createCustom()

fs.writeFileSync('./custom/custom.ts', content, 'utf-8')
console.log(`-+-+-+-+-+-+-+-+-+-+-+ ${path.resolve('./custom/custom.ts')} -+-+-+-+-+-+-+-+-+-+-+-+-+`)
console.log(`${changeColor(content, 96)}`)
console.log(`-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ ${changeColor('生成成功', 92)} +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+`)
process.exit()